package com.diasuite.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;


import com.diasuite.server.ObjectResearch;
import com.diasuite.shared.*;


public class JsonStatusData extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4582051005799298527L;
	AllObjects allDevices = new AllObjects();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		out.println('[');
		String[] parameters = req.getParameterValues("q");
		//String[] equipmentId = req.getParameterValues("id");
		String[] equipmentLoc = req.getParameterValues("location");
		String[] equipmentType = req.getParameterValues("type");
		String[] equipmentId = req.getParameterValues("id");



		if (parameters[0].compareTo("getState")==0){

			AllObjects allobjects;
			allobjects = allDevices.GetNeeded(equipmentLoc[0],equipmentType[0]);

			for (int i=0; i<allobjects.getSwitches().size();i++){
				String Id = equipmentId[0];
				Status equipment = allobjects.getSwitches().get(i);


				if ((equipment.getId().compareTo(Id)==0)){
					out.println("  {");
					out.print("    \"state\": \"");
					out.print(equipment.getState());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");
				}
			}

		}

		else if (parameters[0].compareTo("getDevices")==0){

			AllObjects allobjects;
			allobjects = allDevices.GetNeeded(equipmentLoc[0],equipmentType[0]);
			for (int i=0; i<allobjects.getSwitches().size();i++){
				String location = equipmentLoc[0];
				String type = equipmentType[0];
				Status equipment = allobjects.getSwitches().get(i);



				if ((equipment.getLocation().compareTo(location)==0)&&(equipment.getType().compareTo(type)==0)){
					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");

				}


			}

			for (int i=0; i<allobjects.getSensors().size();i++){
				Sensor equipment = allobjects.getSensors().get(i);
				String location = equipmentLoc[0];
				String type = equipmentType[0];

				if ((equipment.getLocation().compareTo(location)==0)&&(equipment.getType().compareTo(type)==0)){
					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");

				}


			}

			for (int i=0; i<allobjects.getVariators().size();i++){
				Variator equipment = allobjects.getVariators().get(i);
				String location = equipmentLoc[0];
				String type = equipmentType[0];

				if ((equipment.getLocation().compareTo(location)==0)&&(equipment.getType().compareTo(type)==0)){
					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");


				}

			}

			for (int i=0; i<allobjects.getDetectors().size();i++){
				Status equipment = allobjects.getDetectors().get(i);
				String location = equipmentLoc[0];
				String type = equipmentType[0];

				if ((equipment.getLocation().compareTo(location)==0)&&(equipment.getType().compareTo(type)==0)){
					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");

				}


			}




		}

		else if (parameters[0].compareTo("getAllDevices")==0){



			AllObjects allobjects;
			try {
				allDevices= ObjectResearch.Research();
				allDevices.SettingType();


				for (int i=0; i<allDevices.getSwitches().size();i++){
					Status equipment = allDevices.getSwitches().get(i);

					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"state\": \"");
					out.print(equipment.getState());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");




				}

				for (int i=0; i<allDevices.getSensors().size();i++){
					Sensor equipment = allDevices.getSensors().get(i);

					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");




				}

				for (int i=0; i<allDevices.getVariators().size();i++){
					Variator equipment = allDevices.getVariators().get(i);

					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");




				}

				for (int i=0; i<allDevices.getDetectors().size();i++){
					Status equipment = allDevices.getDetectors().get(i);

					out.println("  {");
					out.print("    \"id\": \"");
					out.print(equipment.getId());
					out.println("\",");
					out.print("    \"location\": \"");
					out.print(equipment.getLocation());
					out.println("\",");
					out.print("    \"type\": \"");
					out.print(equipment.getType());
					out.println("\",");
					out.print("    \"name\": \"");
					out.print(equipment.getName());
					out.println("\"");
					out.println("  },");




				}
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}




		}

		else if  (parameters[0].compareTo("set")==0){
			String id = parameters[1]; //id de l'objet dont on veut rÃ©cupÃ©rer les Ã©tats
			String attribut = parameters[2];
			String valeur = parameters[3];

			/*for (int i=0; i<listEquipment.size();i++){
				if (listEquipment.get(i).getId()==id){
					changeStatus(listEquipment.get(i), attribut, valeur);
				}
			}*/

			//TODO fonction qui modifie l'Ã©tat rÃ©el de l'Ã©quipement.


		}

		else {
			out.println("error");
		}


		/*private void changeStatus(Equipment equipement, String attribut, String valeur){
		if (attribut.compareTo("status")==0){
			((Status) equipment).setState(valeur);	
		}
		else if (attribut.compareTo("value")==0){
			((Variator) equipment).setValue(valeur);
		}

		//TODO A complÃ©ter
	}*/

		/*private String requestReader(String request){
		Random rnd = new Random();
		String status = null;
		String[] r = request.split(":");
		if (r[0].compareTo("0")==0){
			status = Boolean.toString(rnd.nextBoolean());
		}
		else if (r[0].compareTo("1")==0){
			status = r[2];
		}
		else
			status = "error";
		return r[1]+":"+status;
	}*/

		out.println(']');
		out.flush();							


	}
}

